const elseTextMW = require('express').Router()
const dotnev = require('dotenv')
const { Configuration, OpenAIApi } = require("openai");
dotnev.config()
elseTextMW.route('/wx')
    .post(async (req, res) => {
        if (req.jsonData.MsgType[0] == 'text') {
            const { jsonData } = req

            const configuration = new Configuration({
                apiKey: process.env.OPENAI_API_KEY,
            });
            const openai = new OpenAIApi(configuration);

            const response = await openai.createCompletion({
                model: "text-davinci-003",
                prompt: jsonData.Content[0],
                temperature: 0.9,
                max_tokens: 400,
                top_p: 1,
                frequency_penalty: 0,
                presence_penalty: 0.6
            });
            console.log(response.data.choices[0].text.trim())
            
            const xmlSendData = require('../../../tools/sendMsg')(jsonData.FromUserName[0], jsonData.ToUserName[0], response.data.choices[0].text.trim())
            res.end(xmlSendData)


        }
    })
module.exports = elseTextMW